Solidity数据类型

您所在的位置:网站首页 solidity 数据类型 Solidity数据类型

Solidity数据类型

2024-07-12 15:06:31| 来源: 网络整理| 查看: 265

Solidity 是一种静态类型语言,这意味着每个变量(状态变量和局部变量)都需要在编译时指定变量的类型。

Solidity 提供了几种基本类型,并且基本类型可以用来组合出复杂类型。

除此之外,类型之间可以在包含运算符号的表达式中进行交互。

“​​undefined​​​”或“​​null​​​”值的概念在Solidity中不存在,但是新声明的变量总是有一个 默认值 ,具体的默认值跟类型相关。 要处理任何意外的值,应该使用错误处理来恢复整个交易,或者返回一个带有第二个​​bool​​ 值的元组表示成功。

bool/布尔类型

布尔值的取值范围为 true 和 false 。

默认值:​​false​​

pragma solidity ^0.8.0; contract TestBool { error NotEqual(bool A,bool B); bool public A; // false bool public B = true; //true // require(A==B,"A not equal B"); if (A != B) { error NotEqual(A,B); } }

运算符:

​​!​​(逻辑非)​​&&​​ (逻辑与, “and” )​​||​​ (逻辑或, “or” )​​==​​ (等于)​​!=​​ (不等于) int、uint/整数类型 int 有符号整型

默认为​​int256​​

不同位长的整形范围如下:

​​int8​​ 取值范围:-(2 ** 7)到 2 ** 7 -1​​int16​​取值范围:-(2 ** 15)到 2 ** 15 -1...​​intX​​取值范围:-(2**​​X​​​-1)到 2**(​​X​​-1) -1​​int256​​取值范围:-(2 ** 255)到 2 ** 255 -1 uint 无符号整型

默认为​​uint256​​

不同位长的整形范围如下:

​​uint8​​取值范围:0 到 2 ** 8 - 1​​uint16​​取值范围:0 到 2 ** 16 - 1...​​uintX​​取值范围:0 到 2 ** ​​X​​ - 1​​uint256​​取值范围:0 到 2 ** 256 - 1

运算符:

比较运算符: = , > (返回布尔值)位运算符: & , | , ^ (异或), ~ (位取反)移位运算符: > (右移位)算数运算符: + , - , 一元运算负 - (仅针对有符号整型), * , / , % (取余或叫模运算) , ** (幂)

对于整形 X,可以使用 type(X).min 和 type(X).max 去获取这个类型的最小值与最大值。

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract TestIntval { int8 public i8 = -1; int public i256 = 456; int public i = -123; // int 等同于 int256 // int 的最大最小值 int public minInt = type(int).min; int public maxInt = type(int).max; uint8 public u8 = 1; uint256 public u256 = 456; uint public u = 123; // uint 等同于 uint256 // uint 的最大最小值 uint public minUInt = type(uint).min; uint public maxUInt = type(uint).max; function mini() public pure returns(uint8){ return type(uint8).max; } }

0.8.0 开始,算术运算有两个计算模式:一个是 “wrapping”(截断)模式或称 “unchecked”(不检查)模式,一个是”checked” (检查)模式。 默认情况下,算术运算在 “checked” 模式下,即都会进行溢出检查,如果结果落在取值范围之外,调用会通过 失败异常 回退。 你也可以通过 ​​unchecked { ... }​​切换到 “unchecked”模式

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract C { function f(uint a, uint b) pure public returns (uint) { // 减法溢出会返回“截断”的结果 unchecked { return a - b; } } function g(uint a, uint b) pure public returns (uint) { // 溢出会抛出异常 return a - b; } }

调用 ​​f(2, 3)​​​将返回 ​​2**256-1,​​​ 而​​ g(2, 3)​​ 会触发失败异常。

​​unchecked​​ 代码块可以在代码块中的任何位置使用,但不可以替代整个函数代码块,同样不可以嵌套。

此设置仅影响语法上位于 ​​unchecked​​ 块内的语句。 在块中调用的函数不会此影响。

address/地址

默认值: 0x0000000000000000000000000000000000000000

运算符:

示例:

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract TestAddress { //与其他机器语言相区别的类型就是这个address 类型,160-bit/20byte address public myAddr = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; //合约自己的地址 address contractAddress = address(this); //跟普通的地址类型一样,但多了两个方法 transfer/send 这两个方法后面章节会讲到 // address sender = payable(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4); //可以使用 balance 属性来查询一个地址的余额 function getBalance() public view returns (uint256, uint256) { require(myAddr.balance < contractAddress.balance, "1 must lg 2"); return (myAddr.balance, contractAddress.balance); } }

bytes/字节数组

在计算机中的最小存储单位是 bit(位)

1byte等于8位Solidity中,byte可以赋值为 16进制数字单引号的单个或多个字符

定长字节数组

bytes1 后面数字1是表示1字节 bytes默认等于bytes1 Bytes2 后面数字2是表示2字节 Bytes3 后面数字3是表示3字节 bytes4 后面数字4是表示4字节

...

bytes32 后面数字32是表示32字节

bytes32 等价于 int256或uint256 的位数

运算符

比较运算符: (返回布尔型)位运算符: &, |, ^ (按位异或), ~ (按位取反)移位运算符: > (右移位)索引访问:如果 x 是 bytesI 类型,那么 x[k] (其中 0 valueType)创建的。

keyType可以是任何内置值类型、字节、字符串或任何约定。

valueType可以是任何类型,包括另一个映射或数组。

映射不可迭代。

// SPDX-License-Identifier: MIT pragma solidity ^0.8.13; contract Mapping { //从地址到uint的映射 mapping(address => uint) public myMap; function get(address _addr) public view returns (uint) { //映射始终返回一个值。 //如果从未设置该值,它将返回默认值。 return myMap[_addr]; } // 更新此地址的值 function set(address _addr, uint _i) public { myMap[_addr] = _i; } function remove(address _addr) public { //将值重置为默认值 delete myMap[_addr]; } } //嵌套 mapping contract NestedMapping { //嵌套映射(从地址映射到另一个映射) mapping(address => mapping(uint => bool)) public nested; function get(address _addr1, uint _i) public view returns (bool) { // 可以从嵌套映射中获取值 return nested[_addr1][_i]; } function set( address _addr1, uint _i, bool _boo ) public { nested[_addr1][_i] = _boo; } // 删除 mapping 的一个元素 function remove(address _addr1, uint _i) public { delete nested[_addr1][_i]; } } array/数组

初始化数组的几种方法

//初始化数组的几种方法 uint256[] public arr; uint256[] public nums = [1, 2, 3]; //定长数组,所有元素初始化为 0 uint256[10] public myFixedSizeArr; //未定义初始值的元素默认为 0 uint256[3] public three = [4, 5]; // 直接打印数组列表; function getNums() external view returns ( uint256[] memory, uint256[] memory, uint256[10] memory, uint256[3] memory ) { return (arr, nums, myFixedSizeArr,three); }

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract Array { //初始化数组的几种方法 uint256[] public arr; uint256[] public nums = [1, 2, 3]; //定长数组,所有元素初始化为 0 uint256[10] public myFixedSizeArr; uint256[3] public three = [4, 5, 6]; // 获取数组长度 function getLen() external view returns (uint256) { return nums.length; } //向数组中添加值 function pushIndex(uint256 _x) external { nums.push(_x); } // 这样删除数组不会改变数组长度,被删除的数字索引值会变成 0 function deleteIndex(uint256 _x) external { delete nums[_x]; } //不改变数组顺序情况下删除数组 //通过循环删除数组索引 [1,2,3,4] => [1,3,4,4] => [1,3,4] function removeIndex(uint256 _x) public { require(_x < nums.length, "out of index"); for (uint256 i = _x; i < nums.length - 1; i++) { nums[i] = nums[i + 1]; } nums.pop(); } // 删除数组会改变元素的顺序,但是节省 gas //[1,2,3,4]=> [1,4,3,4]=>[1,4,3] function removeIndex2(uint256 _x) public { require(_x < nums.length, "out of index"); nums[_x] = nums[nums.length - 1]; nums.pop(); } //移除数组最后一个元素 function pop() external { nums.pop(); } //方法测试 function testRemove() external { nums = [1, 2, 3, 4]; removeIndex(2); assert(nums[0] == 1 && nums[1] == 2); assert(nums[2] == 4); assert(nums.length == 3); } }



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭